/*
 * Interlocked.h
 *
 *  Created on: Nov 26, 2011
 *      Author: Lucifer
 */

#ifndef ATOMIC_H_
#define ATOMIC_H_

namespace BFX
{

// Provides atomic operations for variables that are shared by multiple threads.
class BFX_API Interlocked
{
private:
	Interlocked() {}
	virtual ~Interlocked() {}

public:
	// Increments (increases by one) the value of the specified 32/64 bits variable as an atomic operation.
	static LONG Increment(LONG volatile * Addend);

	// Decrements (decreases by one) the value of the specified 32/64 bits variable as an atomic operation.
	static LONG Decrement(LONG volatile * Addend);

	// Performs an atomic AND operation on the specified LONG values
	static LONG And(LONG volatile * Destination, LONG Value);

	// Performs an atomic AND operation on the specified LONG values
	static LONG Or(LONG volatile * Destination, LONG Value);

	// Sets a 32/64 bits variable to the specified value as an atomic operation.
	static LONG Exchange(LONG volatile * Target, LONG Value);

	// Atomically exchanges a pair of addresses.
	static void* ExchangePointer(void* volatile * Target, void * Value);

	// Performs an atomic compare-and-exchange operation on the specified values.
	static LONG CompareExchange(LONG volatile * Destination, LONG Exchange, LONG Comparand);

	// Performs an atomic compare-and-exchange operation on the specified values.
	static void* CompareExchangePointer(void* volatile * Destination, void* Exchange, void* Comparand);
};

} /* namespace BFX */

#endif /* ATOMIC_H_ */
